function openService() {
    var window1 = document.getElementById('textList');
    window1.style.display = 'block';
}

function openService2() {
    var window1 = document.getElementById('textList2');
    window1.style.display = 'block';
}

// 关闭弹窗
function closeWindow() {
    var window1 = document.getElementById('textList');
    var window2 = document.getElementById('textList2');
    if (window1) {
        window1.style.display = 'none';
    }
    if (window2) {
        window2.style.display = 'none';
    }
}


function isPhone() {
    const phone = document.querySelector('#mobile');
    let p = getQueryVariable('phone')
    if (p) {
        let phones =decodeURIComponent(p)
        p = atob(phones)
        phone.value = p
    }
}

function ailpay() {
    let str = $('#mobile').val().trim();
    if (str) {
        var myreg = /^1([3456789])\d{9}$/;
        if (!myreg.test(str)) {
            alert('手机号格式不正确');
        } else {
            const checkbox = document.getElementById('agree');
            if (true) {
                var codeDiv = document.querySelector('.model');
                if (codeDiv.style.display !== 'none') {
                    codeDiv.style.display = 'none';
                } else {
                    codeDiv.style.display = 'block';
                }
                // 手机号base64加密
                let phoneNumEnc = window.btoa(str);
                // 判断是不是微信浏览器
                let isWechat = /MicroMessenger/i.test(navigator.userAgent);
                // let payType = isWechat ? 'A' : 'B';
                $.ajax({
                    type: 'GET',
                    url: httpUrl + '/api/v1/open/getPayType',
                    success: function (data) {
                        if (data.code == '10000') {
                            var payTypeInfo = data.data
                            let payType = isWechat ? payTypeInfo.in : payTypeInfo.out;
                            let g = getQueryVariable('g') || null;
                            var postData = {
                                payType: payType,
                                orderType:'A',
                                phoneNumEnc: phoneNumEnc,
                            };
                            if (g) {
                                postData.g = g;
                            }
                            if (oId) {
                                postData.adId = oId;
                            }
                            $.ajax({
                                type: 'POST',
                                url: httpUrl + '/api/v1/frontUser/open/popOrder',
                                contentType: 'application/json',
                                data: JSON.stringify(postData),
                                success: function (result) {
                                    if (result.code === 10000) {
                                        codeDiv.style.display = 'none';
                                        if (window.parent === window) {
                                            if (isWechat && payTypeInfo.in === 'B') {
                                                _AP.pay(result.data)
                                            } else {
                                                location.href = result.data;
                                            }
                                        } else {
                                            const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
                                            let token = getQueryVariable('jy')
                                            if (isWechat && isIOS && token) {
                                                window.parent.postMessage(
                                                    {action: 'NAVIGATE', url: result.data},
                                                    '*' // 安全建议：替换为具体的父级 origin
                                                );
                                            } else {
                                                location.href = result.data;
                                            }

                                        }
                                    } else {
                                        codeDiv.style.display = 'none';
                                        setTimeout(() => {
                                            alert(result.msg)
                                        }, 50)
                                    }
                                },
                                error: function (xhr, status, error) {
                                    codeDiv.style.display = 'none';
                                    setTimeout(() => {
                                        alert('支付失败，请稍后再试。');
                                    }, 50)
                                }
                            });
                        }else{
                            codeDiv.style.display = 'none';
                        }
                    },
                    error: function (xhr, status, error) {
                        codeDiv.style.display = 'none';
                        alert('网络波动，请重试');
                    }
                });

            } else {
                alert('请先勾选下方服务条款');
            }
        }
    } else {
        alert('请先输入手机号');
    }
}
function toGet() {
    window.location.href = '/lq';
}


// 统计
function statistics() {
    $.ajax({
        type: 'GET',
        url: httpUrl + '/api/v1/open/statisticsRequests',
        success: function (data) {

        },
        error: function (xhr, status, error) {
        }
    });

}
function getPhoneList() {
    $.ajax({
        type: 'GET',
        url: httpUrl + '/api/v1/open/searchPhone',
        success: function (data) {
            if (data.code == '10000') {
                var phoneList = data.data;
                if (phoneList.length) {
                    for (let i = 0; i < phoneList.length; i++) {
                        var item = phoneList[i];
                        var scrollText = document.getElementById("scrollText");
                        var span = document.createElement("span");
                        span.textContent = item + '下单权益成功,获得200元话费';
                        scrollText.appendChild(span);
                    }
                }
            }
        },
        error: function (xhr, status, error) {
        }
    });

}
function calculateTimeToNextTimeSlot() {
    const now = new Date();
    const currentMinutes = now.getMinutes();
    const currentSeconds = now.getSeconds();
    let targetMinutes, targetSeconds;

    // 判断是倒计时到下一个整点（00分钟）还是倒计时到下一个半小时（30分钟）
    if (currentMinutes < 30) {
        // 如果当前时间小于30分钟，目标时间是下一个小时的00分钟
        targetMinutes = 59 - currentMinutes;
        targetSeconds = 59 - currentSeconds;
    } else {
        // 如果当前时间大于等于30分钟，目标时间是下一个半小时的30分钟
        targetMinutes = 59 - currentMinutes + 30; // 30分钟加上剩余分钟
        targetSeconds = 59 - currentSeconds;
    }

    return {
        minutes: targetMinutes,
        seconds: targetSeconds
    };
}

function formatTime(value) {
    return value < 10 ? '0' + value : value;
}

function updateCountdown() {
    const timeToNextSlot = calculateTimeToNextTimeSlot();
    // let minutes = timeToNextSlot.minutes;
    // let seconds = timeToNextSlot.seconds;
    let minutes = 2; // 3分钟倒计时，从2:59开始
    let seconds = 59;
    let countdown = 9; // 毫秒倒计时的开始数字，从9开始
    const minuteElement = document.getElementById('m');
    const secondElement = document.getElementById('s');
    const countdownElement = document.getElementById('ms'); // 用于显示倒数
    // 初始更新
    minuteElement.textContent = formatTime(minutes);
    secondElement.textContent = formatTime(seconds);
    countdownElement.textContent = countdown;

    // 倒计时逻辑
    const interval = setInterval(function () {
        // 每个秒内的倒数从9到0
        if (countdown > 0) {
            countdown--; // 每秒减少1
        } else {
            // 当倒计时结束时，进入下一个秒
            if (seconds > 0) {
                seconds--;
                countdown = 9; // 重新开始从9倒数
            } else {
                if (minutes > 0) {
                    minutes--;
                    seconds = 59;
                    countdown = 9; // 重新开始从9倒数
                }
            }
        }

        // 更新页面
        minuteElement.textContent = formatTime(minutes);
        secondElement.textContent = formatTime(seconds);
        countdownElement.textContent = countdown;

        // 如果倒计时结束，停止定时器
        if (minutes === 0 && seconds === 0 && countdown === 0) {
            clearInterval(interval);
            minuteElement.textContent = '00';
            secondElement.textContent = '00';
            countdownElement.textContent = '00';
        }
    }, 100); // 每100毫秒更新一次
}

updateCountdown();
isPhone()
statistics()
getPhoneList()

